<?php
/**
 * Created by PhpStorm.
 * User: Hsuin
 * Date: 2017/5/13
 * Time: 10:55
 */

namespace api\controllers;


use common\controllers\ApiCommonCtrl;
use common\services\FavoriteMemberSve;
use Yii;

class FavoriteMemberController extends ApiCommonCtrl
{

    /**
     * 获取列表
     * @return \yii\web\Response
     */
    public function actionList()
    {
        //检查登录
        $this->checkLogin();
        $FavoriteMemberSve = new FavoriteMemberSve();
        $rs = $FavoriteMemberSve->getList(['`favorite_member`.member_id' => $this->TokenObj->member_id], 'create_time DESC', true);
        foreach ($rs['data']['list'] as &$list) {
            $list['avatar'] = $list['avatar'] ? HTTP_DOMAIN_ASSETS . '/' . $list['avatar'] : '';
        }
        return $this->outList($rs);
    }

    //添加
    public function actionAdd()
    {
        //检查登录
        $this->checkLogin();

        $request = Yii::$app->request;
        if ($request->isPost) {
            $data = $request->post();
            $FavoriteMemberSve = new FavoriteMemberSve();
            $data['member_id'] = $this->TokenObj->member_id;
            $rs = $FavoriteMemberSve->add($data);
            return $rs;
        }
    }

    /*
     * 编辑
     */
    public function actionRemove()
    {
        $request = Yii::$app->request;
        if ($request->isPost) {
            //检查登录
            $this->checkLogin();
            $Member = $this->TokenObj;
            $FavoriteMemberSve = new FavoriteMemberSve();

            $memberIds = $request->post('be_favorite_member_ids', '');
            $memberIdArr = explode(',', $memberIds);
            foreach ($memberIdArr as $key=>$value) {
                $rs = $FavoriteMemberSve->remove(['member_id'=>$Member['member_id'],'be_favorite_member_id'=>$value]);
                if ($rs['code'] !== 0) {
                    return $rs;
                }
            }
            return ['msg' => '删除成功', 'code' => 0, 'data' => []];
        }
    }
}